package com.kkd.kkdapp.fragment;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.activity.DealDetailsActivity;
import com.kkd.kkdapp.adapter.ExchangeDetailSimpleAdapter;
import com.kkd.kkdapp.http.entity.DealDetailData;
import com.kkd.kkdapp.http.entity.DealDetailItem;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpClient;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.LogUtil;
import com.kkd.kkdapp.utils.Time_Date;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

public class DealDetailFragment extends Fragment implements KKDScrollListener {

	private final int CODE_ZREO = 1;
	private final int CODE_ELSE = 3;

	private View mContentView;
	ExchangeDetailSimpleAdapter mAdapter;
	private DealDetailsActivity mDealDetailsActivity;
	private String tradeType;
	private int status;
	private KkdXlistView manage_lv;
	private int pageNO = 1;
	private int pageSize = 20;
	View noContent;
	private ImageView mNullImg;
	private TextView mNullTv;
	private boolean REFRESHFLAG = true;
	private boolean isShowToastOnRefresh = false;
	private List<DealDetailItem> datalist = new ArrayList<DealDetailItem>();

	Toast mToast;
	int pageCount;
	String time = "";

	private DealDetailData data;

	private ListView mListView;

	private Handler handler = new Handler() {
		public void handleMessage(android.os.Message msg) {

			Time_Date time_Date;
			String time;

			switch (msg.what) {
			case CODE_ZREO:
				int pageNo = data.getPageNO();
				int pageCount = data.getPageCount();

				if (pageNo > pageCount) {
					manage_lv.stopLoadMore(-1);
					if (REFRESHFLAG) {
						manage_lv.stopLoadMore(1);
					}
				} else {
					manage_lv.setNoMore(false);
					if (pageNO == 1 && datalist != null) {
						datalist.clear();
					}
					LogUtil.e("data1", data.getData1().size() + "");
					datalist.addAll(data.getData1());
					// adapter.appendData(mList, pageNO == 1);
					// adapter.update();
					mAdapter.notifyDataSetChanged();
					if (data.getData1().size() < pageSize) {
						manage_lv.stopLoadMore(1);
					} else {
						manage_lv.stopLoadMore(0);
					}
				}
				time_Date = new Time_Date();
				time = time_Date.get_Year_Day();
				manage_lv.setRefreshTime(time);
				manage_lv.stopRefresh();
				if (REFRESHFLAG) {
					if (datalist != null) {
						if (datalist.size() == 0) {
							mNullImg.setImageResource(R.drawable.img_null);
							mNullTv.setText("您暂无任何交易记录");
							mNullImg.setVisibility(View.VISIBLE);
							mNullTv.setVisibility(View.VISIBLE);
						} else {
							mNullImg.setVisibility(View.GONE);
							mNullTv.setVisibility(View.GONE);
						}
					}
				}
				if (!isShowToastOnRefresh) {
					isShowToastOnRefresh = true;
				}
				break;
			case CODE_ELSE:
				time_Date = new Time_Date();
				time = time_Date.get_Year_Day();
				manage_lv.setRefreshTime(time);
				if (REFRESHFLAG) {
					if (datalist != null) {
						if (datalist.size() == 0) {
							if (msg.arg1 == KkdHttpClient.NO_NET) {
								mNullImg.setImageResource(R.drawable.img_null_net);
								mNullTv.setText("网络错误，请稍候下拉刷新重试");
							} else {
								mNullImg.setImageResource(R.drawable.img_null_net);
								mNullTv.setText("服务异常，请稍候下拉刷新重试");
							}
							mNullImg.setVisibility(View.VISIBLE);
							mNullTv.setVisibility(View.VISIBLE);
						} else {
							mNullImg.setVisibility(View.GONE);
							mNullTv.setVisibility(View.GONE);
						}
					}
				}

				manage_lv.stopLoadMore(1);
				manage_lv.stopRefresh();
				if (datalist.size() == 0) {
					if (msg.arg1 == KkdHttpClient.NO_NET) {
						mNullImg.setImageResource(R.drawable.img_null_net);
						mNullTv.setText("网络错误，请稍候下拉刷新重试");
					} else {
						mNullImg.setImageResource(R.drawable.img_null_net);
						mNullTv.setText("服务异常，请稍候下拉刷新重试");
					}
					mNullImg.setVisibility(View.VISIBLE);
					mNullTv.setVisibility(View.VISIBLE);
				}

				String desc = (String) msg.obj;
				if (isShowToastOnRefresh) {
					if (!TextUtils.isEmpty(desc)) {
						AppUtil.makeToastForCustomer(mDealDetailsActivity, desc);
					}
				} else {
					isShowToastOnRefresh = true;
				}
				break;
			}

		};
	};

	public static DealDetailFragment newInstance(String tradeType, int status) {
		DealDetailFragment fragment = new DealDetailFragment();
		Bundle mBundle = new Bundle();
		mBundle.putString("tradeType", tradeType);
		mBundle.putInt("status", status);
		fragment.setArguments(mBundle);
		return fragment;
	}

	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mDealDetailsActivity = (DealDetailsActivity) activity;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mContentView = inflater.inflate(
				R.layout.fragment_classic_header_with_list_view, null);
		tradeType = getArguments().getString("tradeType");
		status = getArguments().getInt("status");
		mNullImg = (ImageView) mContentView.findViewById(R.id.id_no_content);
		mNullTv = (TextView) mContentView.findViewById(R.id.id_no_content_text);

		manage_lv = (KkdXlistView) mContentView
				.findViewById(R.id.deal_detail_list);
		manage_lv.setKKDScrollListener(this);
		mAdapter = new ExchangeDetailSimpleAdapter(getActivity(), datalist);
		manage_lv.setAdapter(mAdapter);
		manage_lv.startRefresh();
		ListView mListView = manage_lv.mListView;
		mListView.setDividerHeight(0);
		requestDealDetails(tradeType, status);
		return mContentView;
	}

	public void requestDealDetails(String tradeType, int status) {
		KkdHttpMagager.getInstance().getDealDetailsManager(pageNO, pageSize,
				tradeType, status, DealDetailData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {

						data = (DealDetailData) responseBody;

						handler.sendEmptyMessage(CODE_ZREO);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						Message msg = Message.obtain();
						msg.what = CODE_ELSE;
						msg.obj = desc;
						msg.arg1 = statusCode;
						handler.sendMessage(msg);
					}
				});
	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		requestDealDetails(tradeType, status);
		REFRESHFLAG = true;
	}

	@Override
	public void onLoadMore() {
		pageNO++;
		REFRESHFLAG = false;
		requestDealDetails(tradeType, status);

	}

	public void showToast(String hintLanguage) {
		AppUtil.makeToastForCustomer(mDealDetailsActivity, hintLanguage);
	}

	@Override
	public void onResume() {
		super.onResume();
		if (mToast != null) {
			mToast.cancel();
			mToast = null;
		}
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		if (mToast != null) {
			mToast.cancel();
			mToast = null;
		}
	}
}
